clear
set more off

use $tempdir/panel_trends_30yr_income.dta, clear	
	
keep if race==1 

gen maleobs = 1 if sex == 1
gen femaleobs = 1 if sex == 2

gen maleobs_married = 1 if sex == 1 & ever_married == 1
gen femaleobs_married = 1 if sex == 2 & ever_married == 1

gen maleobs_spouseinrange = 1 if spouseage >= 30 & spouseage <= 45 & sex == 1
gen femaleobs_spouseinrange = 1 if spouseage >= 30 & spouseage <= 45 & sex == 2

collapse (count) maleobs* femaleobs*, by(year2 age)

save $tempdir/observations_by_age.dta, replace

*************************************************
*Show how observations are distributed by age
*************************************************

use $tempdir/observations_by_age.dta, clear

foreach g in male female{
	gen `g'obs_unmarried = `g'obs - `g'obs_married
	gen `g'obs_spouseoutrange = `g'obs_married - `g'obs_spouseinrange
	}

gen zero = 0

foreach y in $yearlist{

twoway rbar zero femaleobs_spouseinrange age if year2 == `y', barwidth(0.6) || /*
	*/ rbar femaleobs_spouseinrange femaleobs_married age if year2 == `y', barwidth(0.6) || /*
	*/ rbar femaleobs_married femaleobs age if year2 == `y', barwidth(0.6)/*
	*/ xtitle("Age") ytitle("Observations") title("Women, Ages 30-45") name(women_age, replace)  /*
	*/ legend(label(1 "Married, Spouse in Range") label(2 "Married, Spouse out of Range") label(3 "Unmarried") cols(1))

twoway rbar zero maleobs_spouseinrange age if year2 == `y', barwidth(0.6) || /*
	*/ rbar maleobs_spouseinrange maleobs_married age if year2 == `y', barwidth(0.6) || /*
	*/ rbar maleobs_married maleobs age if year2 == `y', barwidth(0.6) /*
	*/ xtitle("Age") ytitle("Observations") title("Men, Ages 30-45") name(men_age, replace)  /*
	*/ legend(label(1 "Married, Spouse in Range") label(2 "Married, Spouse out of Range") label(3 "Unmarried") cols(1))

graph combine men_age women_age, ycommon title("`y' Sample Age Distributions")
graph export "$outputdir/agedist_`y'.pdf", replace
}







